﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Data;
using System.Data.SqlClient;
//using System.Web.Caching;

/// <summary>
/// QueryFlights 的摘要说明
/// </summary>
[WebService(Namespace = "http://localhost/crsi")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Query : System.Web.Services.WebService {

    public Query()
    {
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public DataSet QueryFlights(string ori, string des, int lang, DateTime flt_date, Int16 seats, Int16 seg_sn, Int16 seg_total, string user, string sign,out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

    //    //此接口已被取代！
        ret_msg = "This service has been suspended!";
        ret_code = -1;
        return ds;

    //    //将复杂查询分解为简单、原子查询，此功能暂停！
    //    //ret_msg = "Service suspended!";
    //    //ret_code = -1;
    //    //return ds;

    //    try
    //    {
    //        cmd.CommandText = "sali_user0_get_pwd_hash";
    //        para = cmd.Parameters.AddWithValue("@uid", user);
    //        ret_code = TinyDAL.Exec(ref cmd, ref dt);
    //        ret_msg = "";
    //    }
    //    catch (Exception ex)
    //    {
    //        ret_msg = ex.Message;
    //        ret_code = -1;
    //        return ds;
    //    }
    //    if(dt.Rows.Count!=1)
    //    {
    //        ret_msg="User Not found!";
    //        ret_code=-1;
    //        return ds;
    //    }
    //    string inbuf = ori + "|" + des + "|"+flt_date.ToString("yyyyMMdd")+"|" + dt.Rows[0][0].ToString();
    //    string outbuf = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(inbuf, "MD5");
    //    if (outbuf != sign)
    //    {
    //        ret_msg = "Illegal!";
    //        ret_code = -1;
    //        return ds;
    //    }


    //    cmd.Parameters.Clear();
    //    try
    //    {
    //        cmd.CommandText = "crsi_query_flights";
    //        para = cmd.Parameters.AddWithValue("@ori", ori);
    //        para = cmd.Parameters.AddWithValue("@des", des);
    //        para = cmd.Parameters.AddWithValue("@lang", lang);
    //        para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
    //        para = cmd.Parameters.AddWithValue("@seats", seats);
    //        para = cmd.Parameters.AddWithValue("@seg_sn", seg_sn);
    //        para = cmd.Parameters.AddWithValue("@seg_total", seg_total);
    //        para = cmd.Parameters.AddWithValue("@user", user);
    //        //para = cmd.Parameters.AddWithValue("@pwd", password);
    //        para = cmd.Parameters.AddWithValue("@ip_addr",HttpContext.Current.Request.UserHostAddress);
    //        ret_code=TinyDAL.Exec(ref cmd, ref ds);
    //        ret_msg = "";
    //    }
    //    catch (Exception ex)
    //    {
    //        ret_msg = ex.Message;
    //        ret_code = -1;
    //    }
    //    return ds;

    }

    [WebMethod]
    public DataSet QueryAvailFlights(string ori, string des, int lang, DateTime flt_date, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = ori + "|" + des + "|" + flt_date.ToString("yyyyMMdd");
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }
        string bufKey;
        DataSet bufSet;
        
        //先查缓冲数据库，如有有效记录直接返回
        bufKey = "av_flts:"+ori + des + flt_date.ToShortDateString() +"-"+ lang.ToString();
        bufSet = (DataSet)MyCache.GetCache(bufKey);
        if (bufSet != null)
        {
            return bufSet;
        }
        
        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "sali_query_get_avail_flts";
            para = cmd.Parameters.AddWithValue("@ori", ori);
            para = cmd.Parameters.AddWithValue("@des", des);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
            para = cmd.Parameters.AddWithValue("@user", user);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
            return ds;
        }

        MyCache.SetCache(bufKey, ds, 60, 0);

        return ds;

    }

    [WebMethod]
    public DataSet QueryFltAvailFares(string airline, string flt_no, DateTime flt_date, string ori, string des, int lang, Int16 seats, Int16 seg_sn, Int16 seg_total, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = airline + "|" + flt_no + "|" + flt_date.ToString("yyyyMMdd") + "|" + ori + "|" + des;
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }
        string bufKey;
        DataSet bufSet;

        //先查缓冲数据库，如有有效记录直接返回
        bufKey = "flt_fares:"+airline + flt_no + flt_date.ToShortDateString() +ori+des+ lang.ToString()+"-"+seats.ToString()+"-"+seg_sn.ToString()+"-"+seg_total.ToString();
        bufSet = (DataSet)MyCache.GetCache(bufKey);
        if (bufSet != null)
        {
            return bufSet;
        }

        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "sali_query_get_flt_avail_fares";
            para = cmd.Parameters.AddWithValue("@airline", airline);
            para = cmd.Parameters.AddWithValue("@flt_no", flt_no);
            para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
            para = cmd.Parameters.AddWithValue("@ori", ori);
            para = cmd.Parameters.AddWithValue("@des", des);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@seats", seats);
            para = cmd.Parameters.AddWithValue("@seg_sn", seg_sn);
            para = cmd.Parameters.AddWithValue("@seg_total", seg_total);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", password);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }

        MyCache.SetCache(bufKey, ds, 60, 0);

        return ds;

    }

    [WebMethod]
    public DataSet QueryFareDetail(string airline, string fare_code, string ori, string des,string tax_type,int dbf,int lang, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = airline + "|" + fare_code;
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }
        string bufKey;
        DataSet bufSet;

        //先查缓冲数据库，如有有效记录直接返回
        bufKey = "fare_detail:" + airline + fare_code + ori + des + tax_type + dbf.ToString() + "-" + lang.ToString();
        bufSet = (DataSet)MyCache.GetCache(bufKey);
        if (bufSet != null)
        {
            return bufSet;
        }

        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "sali_query_get_fare_details";
            para = cmd.Parameters.AddWithValue("@airline", airline);
            para = cmd.Parameters.AddWithValue("@fare_code", fare_code);
            para = cmd.Parameters.AddWithValue("@ori", ori);
            para = cmd.Parameters.AddWithValue("@des", des);
            para = cmd.Parameters.AddWithValue("@tax_type", tax_type);
            para = cmd.Parameters.AddWithValue("@dbf", dbf);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", password);
            //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }

        MyCache.SetCache(bufKey, ds, 60, 0);

        return ds;

    }

    //[WebMethod]
    //private int QueryFareDiscount(string airline, string dcp_id, string ori, DateTime dep_time, string user, string password, out float percentage, out string ret_msg)
    //{
    //    考虑到性能和实用性，暂时取消票价DCP功能！
    //    int ret_code;
    //    SqlCommand cmd = new SqlCommand();
    //    SqlParameter para,para2;
    //    percentage = 1.0F;
    //    try
    //    {
    //        cmd.CommandText = "crsi_get_fare_dcp_percentage";
    //        para = cmd.Parameters.AddWithValue("@airline", airline);
    //        para = cmd.Parameters.AddWithValue("@dcp_id", dcp_id);
    //        para = cmd.Parameters.AddWithValue("@ori", ori);
    //        para = cmd.Parameters.AddWithValue("@dep_time", dep_time);
    //        para = cmd.Parameters.AddWithValue("@user", user);
    //        para = cmd.Parameters.AddWithValue("@pwd", password);
    //        para2 = cmd.Parameters.AddWithValue("@percentage", percentage);
    //        para2.Direction = ParameterDirection.Output;
    //        para2.SqlDbType = SqlDbType.Real;
    //        //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
    //        ret_code = TinyDAL.Exec(ref cmd);
    //        ret_msg = "";
    //        if (ret_code == 0)
    //        {
    //            percentage = Convert.ToSingle(para2.Value);
                
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        ret_msg = ex.Message;
    //        ret_code = -1;
    //    }
    //    return ret_code;
    //}


    [WebMethod]
    public DataSet QueryAvailFares(string ori, string des, int lang, DateTime flt_date, Int16 seats, Int16 seg_sn, Int16 seg_total, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50 || ori.Length<3 || ori.Length>4 || des.Length<3 || des.Length>4)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = ori + "|" + des + "|" + flt_date.ToShortDateString() + "|" + seats.ToString() + "|" + seg_sn.ToString() + "|" + seg_total.ToString() + "|" + lang.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }
        string bufKey;
        DataSet bufSet;

        //先查缓冲数据库，如有有效记录直接返回
        bufKey = "av_fares:" + ori + des + flt_date.ToShortDateString() + "-"+ seats.ToString() + "-" + seg_sn.ToString() + "-" + seg_total.ToString()+"-"+lang.ToString();
        bufSet = (DataSet)MyCache.GetCache(bufKey);
        if (bufSet != null)
        {
            return bufSet;
        }

        //如无缓冲数据，则查核心库，并将结果插入缓冲（通过目录表避免两个进程插入相同的查询结果！）
        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "sali_query_get_avail_fares";
            para = cmd.Parameters.AddWithValue("@ori", ori);
            para = cmd.Parameters.AddWithValue("@des", des);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@seg_sn", seg_sn);
            para = cmd.Parameters.AddWithValue("@seg_total", seg_total);
            para = cmd.Parameters.AddWithValue("@seats", seats);
            para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
            para = cmd.Parameters.AddWithValue("@user", user);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
            return ds;
        }

        MyCache.SetCache(bufKey, ds, 60, 0);

        return ds;

    }

    [WebMethod]
    public DataSet QueryFareAvailFlts(string airline, string fare_code, string cabin, string seat_class, int dbf, string ori, string des, int lang, DateTime flt_date, Int16 seats, Int16 seg_sn, Int16 seg_total, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50 || ori.Length < 3 || ori.Length > 4 || des.Length < 3 || des.Length > 4)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = airline + "|" + fare_code + "|" + seat_class.ToString() + "|" + seats.ToString() + "|" + flt_date.ToShortDateString() + "|" + dbf.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }
        string bufKey;
        DataSet bufSet;



        //先查缓冲数据库，如有有效记录直接返回
        bufKey = "av_fares:" + airline + fare_code +seat_class+ori+des+ flt_date.ToShortDateString() +"-"+dbf.ToString()+ "-" + seats.ToString() + "-" + seg_sn.ToString() + "-" + seg_total.ToString() + "-" + lang.ToString();
        bufSet = (DataSet)MyCache.GetCache(bufKey);
        if (bufSet != null)
        {
            return bufSet;
        }

        //如无缓冲数据，则查核心库，并将结果插入缓冲（通过目录表避免两个进程插入相同的查询结果！）
        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "sali_query_get_fare_avail_flts";
            para = cmd.Parameters.AddWithValue("@airline", airline);
            para = cmd.Parameters.AddWithValue("@fare_code", fare_code);
            para = cmd.Parameters.AddWithValue("@cabin", cabin);
            para = cmd.Parameters.AddWithValue("@class", seat_class);
            para = cmd.Parameters.AddWithValue("@dbf", dbf);
            para = cmd.Parameters.AddWithValue("@ori", ori);
            para = cmd.Parameters.AddWithValue("@des", des);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@flt_date", flt_date);
            para = cmd.Parameters.AddWithValue("@seg_sn", seg_sn);
            para = cmd.Parameters.AddWithValue("@seg_total", seg_total);
            para = cmd.Parameters.AddWithValue("@seats", seats);
            para = cmd.Parameters.AddWithValue("@user", user);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
            return ds;
        }

        MyCache.SetCache(bufKey, ds, 60, 0);

        return ds;

    }
}

